package com.ruoyi.lm.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lm.domain.LmUser;
import com.ruoyi.lm.domain.LmWithdrawrecord;
import com.ruoyi.lm.service.ILmUserService;
import com.ruoyi.lm.service.ILmUsersignService;
import com.ruoyi.lm.service.ILmWithdrawrecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户提现记录Controller
 * 
 * @author ruoyi
 * @date 2020-09-02
 */
@Controller
@RequestMapping("/phone/lm/withdrawrecord")
public class LmWithdrawrecordControllerPhone extends BaseController
{
    private String prefix = "lm/withdrawrecord";

    @Autowired
    private ILmWithdrawrecordService lmWithdrawrecordService;

    @Autowired
    private ILmUserService lmUserService;

    /**
     * 查询用户提现记录列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LmWithdrawrecord lmWithdrawrecord)
    {
        startPage();
        List<LmWithdrawrecord> list = lmWithdrawrecordService.selectLmWithdrawrecordList(lmWithdrawrecord);
        return getDataTable(list);
    }

    /**
     * 导出用户提现记录列表
     */
    @Log(title = "用户提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LmWithdrawrecord lmWithdrawrecord)
    {
        List<LmWithdrawrecord> list = lmWithdrawrecordService.selectLmWithdrawrecordList(lmWithdrawrecord);
        ExcelUtil<LmWithdrawrecord> util = new ExcelUtil<LmWithdrawrecord>(LmWithdrawrecord.class);
        return util.exportExcel(list, "withdrawrecord");
    }

    /**
     * 新增保存用户提现记录
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody LmWithdrawrecord lmWithdrawrecord)
    {
        //默认申请中
        lmWithdrawrecord.setWithdrawStatus("申请中");
        String userId = lmWithdrawrecord.getUserId();

        LmUser userP = lmUserService.selectLmUserById(userId);
        //保存邀请人用户余额
        double userBalance = Double.valueOf(userP.getUserBalance());
        userBalance -= lmWithdrawrecord.getWithdrawSum();
        userP.setUserBalance(String.valueOf(userBalance));
        lmUserService.updateLmUser(userP);

        return toAjax(lmWithdrawrecordService.insertLmWithdrawrecord(lmWithdrawrecord));
    }

    /**
     * 修改保存用户提现记录
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody LmWithdrawrecord lmWithdrawrecord)
    {
        return toAjax(lmWithdrawrecordService.updateLmWithdrawrecord(lmWithdrawrecord));
    }

    /**
     * 删除用户提现记录
     */
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lmWithdrawrecordService.deleteLmWithdrawrecordByIds(ids));
    }
}
